package com.ahora.org.controller.servlet.admi;

import java.io.IOException;
import java.net.InetAddress;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

import com.ahora.org.ejb.dao.util.ConexionBD;
import com.ahora.org.ejb.dao.util.Util;
import com.ahora.org.ejb.jpa.entities.Usuario;

/**
 * Servlet implementation class ReportingRequisitosRegistrados
 */
@WebServlet("/ReportingRequisitosRegistrados")
public class ReportingRequisitosRegistrados extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Usuario objUsuario=null;
	private Connection connection = ConexionBD.getConnection(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReportingRequisitosRegistrados() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Content-Disposition", "attachment; filename=\"Administrador_Reporte_Requisitos_Registrados.pdf\";");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("application/pdf");
        
       ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		HttpSession sessionhttp = (HttpSession) context.getSession(true);
		if (sessionhttp.getAttribute("usuarioLogin") == null) {
			objUsuario = new Usuario();
		} else {
			objUsuario = (Usuario) sessionhttp.getAttribute("usuarioLogin");}
		
		System.out.println("MI USUARIO ES *******************************************************************************");
		System.out.println("USUARIO ==>" + objUsuario.getUsuario());
		/****************************************************************************************************************/
		//String nombrePC=InetAddress.getLocalHost().getCanonicalHostName();
		
		ServletOutputStream out = response.getOutputStream();
		InetAddress ip = InetAddress.getLocalHost();
		InetAddress addr = InetAddress.getByName(ip.getHostAddress()); 
		try {
			JasperReport reporte= (JasperReport) JRLoader.loadObject(getServletContext().getRealPath("reporting/admi/rptRequisitosRegistrados.jasper"));
			Map parametros= new HashMap();
			parametros.put("nombreUsuario", objUsuario.getNombres()+" "+objUsuario.getApellPater()+" "+objUsuario.getApellMater());
			parametros.put("nombrePC", addr.getHostName());
			parametros.put("NombreEntidad",  Util.retornarNombreEntidad());
			
			JasperPrint jasper= JasperFillManager.fillReport(reporte, parametros,connection);
			JRExporter exporter= new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasper);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
			exporter.exportReport();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
